import qs from "../../lib/qs/qs";
import to from "../../lib/await-to-js/await-to-js.es5";

import { tabbar_route_map, linkToTabbar } from "../../utils/route.js";
import { API_URL } from "../../api/config.js";
import api from "../../api/api.js";
import http from "../../utils/request.js";
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    washMode: '',
    washertypeInfo: {}, //洗衣机信息
    washModeInfo: {}, //洗衣模式信息
    washerInfo: {}, //
    current: "微信支付",
    payModes: [
      { id: 1, name: "微信支付" },
      { id: 2, name: "余额" }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log("options", options);
    const washMode = options.washMode;
    const washerInfo = JSON.parse(options.washerInfo);
    console.log("washerInfo", washerInfo);
    this.setData({
      washMode: washMode
    });
    this.setWasheModeInfo(washMode, washerInfo);
  },

  setWasheModeInfo(washMode, washerInfo) {
    //list_washmode
    const washertypeInfo = washerInfo.washertype;
    const washModeInfo = washerInfo.list_washmode.filter(item => {
      console.log(`item.mode_idx:${item.mode_idx} , washMode:${washMode}`);
      return item.mode_idx == washMode;
    })[0];
    washModeInfo.washImgUrl =
      "../../images/wash" + washModeInfo.mode_cmd + ".png";

    console.log("washModeInfo", washModeInfo);

    this.setData({
      washertypeInfo: washertypeInfo,
      washModeInfo: washModeInfo,
      washerInfo: washerInfo
    });
  },

  // async loadDeviceMode(id) {
  //   const [err, result] = await to(wx.pro.request({ id }));

  //   if (err) {
  //     console.log(err);
  //     wx.showToast({
  //       title: err,
  //       icon: "none"
  //     });
  //     return;
  //   }

  //   console.log(result);
  // },

  payTypeChange({ detail = {} }) {
    // console.log(detail);
    this.setData({
      current: detail.value
    });
  },

  async payTap() {
    console.log("支付中....");
    const did = app.globalData.did;
    const token = wx.getStorageSync("token");
    const _this = this;
    const washMode=this.data.washMode
    let payMode = this.data.payModes.filter(
      item => item.name == this.data.current
    )[0];
    console.log(payMode);
    //http://iot.inbo.vip/wx/pay_mini
    console.log(
      `发起请求API->${api.WxPay},发起支付参数->设备did:${did},洗衣模式:${washMode} 支付方式:${payMode.id} token:${token}`
    );
    // 测试did  did:'B220014', did:'B110002',
    const [err, data] = await to(
      http.req({
        url: api.WxPay,
        method: "POST",
        data: {
          openid: token,
          did,
          mode: washMode
        }
      })
    );
    console.log(`err:${err} , data: ${JSON.stringify(data)}`);
    if (err) {
      wx.showModal({
        title: "支付失败",
        content: "发起支付请求失败",
        showCancel: false
      });
    } else {
      console.log("result", data);
      const { appId, nonceStr, paySign, signType, timeStamp } = data;
      console.log(
        `appId, nonceStr, paySign, signType, timeStamp:${appId}, ${nonceStr}, ${paySign}, ${signType}, ${timeStamp}`
      );
      console.log(`package:${data.package}`);
      // appId,
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonceStr,
        package: data.package,
        signType: "MD5",
        paySign: data.paySign,
        success: function(res) {
          console.log("success", res);
          _this.paySuccess();
        },
        fail: function(res) {
          console.log("fail", res);
          _this.payFail();
        },
        complete: function(res) {
          console.log("complete", res);
        }
      });
    }
  },
  paySuccess() {
    console.log("支付成功,返回wash页面");
    wx.showModal({
      title: "支付成功",
      showCancel: false,
      success() {
        wx.navigateBack({
          delta: 1,
          success() {
            app.globalData.refreshWash = true;
          }
        });
      }
    });
  },
  payFail() {
    wx.showModal({
      title: "支付失败",
      content: "支付失败,请稍后重试",
      showCancel: false
    });
  }
});
